package com.hqw.mindanews.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.bmob.v3.datatype.BmobFile;
import cn.pedant.SweetAlert.SweetAlertDialog;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.hqw.mindanews.R;
import com.hqw.mindanews.bean.NewsMsg;
import com.hqw.mindanews.log.Log;
import com.hqw.mindanews.utils.LruImageCache;
import com.hqw.mindanews.utils.StringUtil;
import com.hqw.mindanews.utils.VolleyUtil;

public class NewsAdapter extends BaseAdapter {

	private List<NewsMsg> mList;
	private LayoutInflater mInflater;
	private ImageLoader imageLoader;
	private Context mContext;

	public NewsAdapter(Context context, List<NewsMsg> data) {
		mList = data;
		mContext = context;
		this.imageLoader = new ImageLoader(VolleyUtil.getQueue(context),
				new LruImageCache());
		mInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {

		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public static void setMargins(View v, int l, int t, int r, int b) {
		if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
			ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v
					.getLayoutParams();
			p.setMargins(l, t, r, b);
			v.requestLayout();
		}
	}
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		int imageLength = mList.get(position).getBmobFiles().size();
		Log.i("bmob","第"+position+"个的图片长度为" +imageLength);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_news_list, null);
			viewHolder.userIcon = (ImageView) convertView
					.findViewById(R.id.user_icon);
			viewHolder.newsTime = (TextView) convertView.findViewById(R.id.news_time);
			viewHolder.userName = (TextView) convertView
					.findViewById(R.id.user_name);
			viewHolder.msgContent = (TextView) convertView
					.findViewById(R.id.msg_content);

			viewHolder.viewGroup = (LinearLayout) convertView
					.findViewById(R.id.viewGroup);
//			viewHolder.imageview0 = (ImageView) convertView
//					.findViewById(R.id.imageView1);
//			viewHolder.imageview1 = (ImageView) convertView
//					.findViewById(R.id.imageView2);
//			viewHolder.imageview2 = (ImageView) convertView
//					.findViewById(R.id.imageView3);
//			viewHolder.imageview0.setVisibility(View.GONE);
//			viewHolder.imageview1.setVisibility(View.GONE);
//			viewHolder.imageview2.setVisibility(View.GONE);
			

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		
		viewHolder.imageview0 = (ImageView) convertView
				.findViewById(R.id.imageView1);
		viewHolder.imageview1 = (ImageView) convertView
				.findViewById(R.id.imageView2);
		viewHolder.imageview2 = (ImageView) convertView
				.findViewById(R.id.imageView3);
		viewHolder.imageview0.setVisibility(View.GONE);
		viewHolder.imageview1.setVisibility(View.GONE);
		viewHolder.imageview2.setVisibility(View.GONE);
		
		if (imageLength >= 1) {
			viewHolder.imageview0.setVisibility(View.VISIBLE);
			mList.get(position).getBmobFiles().get(0).loadImage(mContext, viewHolder.imageview0);
			//mList.get(position).getBmobFiles().get(0).loadImageThumbnail(mContext, viewHolder.imageview0, viewHolder.imageview0.getLayoutParams().width, viewHolder.imageview1.getLayoutParams().height, 90);
			if (imageLength >= 2) {
				Log.i("bmob","长度是"+imageLength+"标题是"+mList.get(position).getContent());
				 
				Log.i("bmob","我是"+position+"个  创建了第二个图片");
				viewHolder.imageview1.setVisibility(View.VISIBLE);
				mList.get(position).getBmobFiles().get(1).loadImage(mContext, viewHolder.imageview1);
				
				//mList.get(position).getBmobFiles().get(1).loadImageThumbnail(mContext, viewHolder.imageview1, viewHolder.imageview1.getLayoutParams().width, viewHolder.imageview1.getLayoutParams().height, 90);
				if (imageLength >= 3) {
					Log.i("bmob","我是"+position+"个  创建了第三个图片");
					viewHolder.imageview2.setVisibility(View.VISIBLE);
					mList.get(position).getBmobFiles().get(2).loadImage(mContext, viewHolder.imageview2);
					//mList.get(position).getBmobFiles().get(1).loadImageThumbnail(mContext, viewHolder.imageview2, viewHolder.imageview2.getLayoutParams().width, viewHolder.imageview2.getLayoutParams().height, 90);
				}
			}
		}
		
		viewHolder.userIcon.setImageResource(R.drawable.default_round_head);
		BmobFile bmobFile = mList.get(position).getUser().getBmobFile();
		if (bmobFile!=null) {
			mList.get(position).getUser().getBmobFile().loadImage(mContext, viewHolder.userIcon);
		}
	
		viewHolder.userIcon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new SweetAlertDialog(mContext).setContentText("点击了头").show();
				
			}
		});
		List<BmobFile> bmobFiles = mList.get(position).getBmobFiles();
		for (int i = 0; i < imageLength; i++) {

			if (i == 0) {
//				String URL0 = BmobProFile.getInstance(mContext).signURL(
//						bmobFiles.get(i).getFilename(),
//						bmobFiles.get(i).getFileUrl(mContext),
//						Constant.BMOB_ACCESS_KEY, 0, null);
				//viewHolder.imageview0.setTag(URL0);
				//setImage(viewHolder.imageview0, URL0);
			} else if (i == 1) {
//				String URL1 = BmobProFile.getInstance(mContext).signURL(
//						bmobFiles.get(i).getFilename(),
//						bmobFiles.get(i).getFileUrl(mContext),
//						Constant.BMOB_ACCESS_KEY, 0, null);
				//viewHolder.imageview1.setTag(URL1);
				//setImage(viewHolder.imageview1, URL1);
			} else if (i == 2) {
//				String URL2 = BmobProFile.getInstance(mContext).signURL(
//						bmobFiles.get(i).getFilename(),
//						bmobFiles.get(i).getFileUrl(mContext),
//						Constant.BMOB_ACCESS_KEY, 0, null);
				//viewHolder.imageview2.setTag(URL2);
				//setImage(viewHolder.imageview2, URL2);
			}

		}

		viewHolder.userName.setText(mList.get(position).getUser().getUsername());
		viewHolder.msgContent.setText(mList.get(position).getContent());
		viewHolder.newsTime.setText(mList.get(position).getCreatedAt());
		return convertView;
	}

	void setImage(ImageView imageView, String imageUrl) {

		ImageContainer container;
		
		try {

			if (imageView.getTag() != null) {
				container = (ImageContainer) imageView.getTag();
				container.cancelRequest();
			}else {
				
			}
		} catch (Exception e) {

		}

		ImageListener listener = ImageLoader.getImageListener(imageView,
				R.drawable.bg_transparent, R.drawable.bg_transparent);
		
		container = imageLoader.get(StringUtil.preUrl(imageUrl), listener);
		
		imageView.setTag(container);

	}

	class ViewHolder {
		public TextView userName;
		public TextView msgContent;
		public ImageView userIcon;
		public ImageView imageview0 = null;
		public ImageView imageview1 = null;
		public TextView newsTime;
		public ImageView imageview2 = null;
		public LinearLayout viewGroup;
	}

}
